<!DOCTYPE html>
<html lang="en">
<title>订单管理</title>
<#import "/layout/layout.html" as body/>
<@body.page>
<style>
    a{
        color: #666;
    }
    .tcdPageCode {
        padding: 15px 20px;
        text-align: left;
        color: #ccc;
    }

    .tcdPageCode a {
        display: inline-block;
        color: #428bca;
        height: 25px;
        line-height: 25px;
        padding: 0 10px;
        border: 1px solid #ddd;
        margin: 0 2px;
        border-radius: 4px;
        vertical-align: middle;
    }

    .tcdPageCode a:hover {
        text-decoration: none;
        border: 1px solid #428bca;
    }

    .tcdPageCode span.current {
        display: inline-block;
        height: 25px;
        line-height: 25px;
        padding: 0 10px;
        margin: 0 2px;
        color: #fff;
        background-color: #428bca;
        border: 1px solid #428bca;
        border-radius: 4px;
        vertical-align: middle;
    }

    .tcdPageCode span.disabled {
        display: inline-block;
        height: 25px;
        line-height: 25px;
        padding: 0 10px;
        margin: 0 2px;
        color: #bfbfbf;
        background: #f2f2f2;
        border: 1px solid #bfbfbf;
        border-radius: 4px;
        vertical-align: middle;
    }

    .chosen-container {
        min-width: 160px !important;
    }
    .modal-loading { position: fixed; top: 0; left: 0; width: 100%; height: 100%; z-index: 10000; background: rgba(0, 0, 0, .5) url("${baseUrl}img/loading.gif") no-repeat center; }
</style>
<div class="modal-loading"></div>

<div class="wrapper wrapper-content animated fadeIn">
    <div class="tabs-container">
        <ul class="nav nav-tabs" id="order_type">
            <li class="active"><a data-toggle="tab" href="#tab-1" name="tab-1" aria-expanded="false" id="part_refund">全额退款</a></li>
            <li class=""><a data-toggle="tab" href="#tab-2" name="tab-2" aria-expanded="false" id="part_refund">部分退款</a></li>
        </ul>
        <div class="tab-content">
            <!-- 退款start -->
             <div id="tab-1" class="tab-pane active" >
                <div class="panel-body">
                	<form class="form-inline" role="form">
	                     <div class="form-group margin-left">
	                          <label for="daterange2">订单号：</label>
	                          <input class="form-control" id="orderNumber" name="orderNumber" type="text" style="width:150px" placeholder="订单号"/>
	                     </div>
	                     <button type="button" id="btn_tuikuan" class="btn btn-outline btn-primary">退款</button>
                    </form>
                </div>
             </div>	
            <!-- 退款end -->
            <!-- 退款start -->
             <div id="tab-2" class="tab-pane" >
                <div class="panel-body">
                	<form class="form-inline" role="form">
	                     <div class="form-group margin-left">
	                          <label for="daterange2">订单号：</label>
	                          <input class="form-control" id="part_orderNumber" name="part_orderNumber" type="text" style="width:150px" placeholder="订单号"/>
	                     </div>
	                     <button type="button" id="btn_part" class="btn btn-outline btn-primary">查询</button>
                    </form>
                    <hr>
                    <div class="table-responsive">
                        <!-- dataTables-example -->
                        <table class="table table-hover table-center dataTables-example">
                            <thead>
                            <tr>
                                <th>交易编号</th>
                                <th id="tk_th">下订单时间</th>
                                <th>订单号</th>
                                <th>实付</th>
                                <th>退款金额</th>
                                <th>备注说明</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <!-- 页面加载的数据  -->
                            <tbody id="td1">
                            </tfoot>
                        </table>
                    </div>
                </div>
             </div>	
            <!-- 退款end -->
        </div>
        <h5 style="color: red;">注意：私教订单退款后会自动取消订单，导致该订单已销课的课时费无法结算，请谨慎处理</h5>
    </div>
</div>

<!-- mode start -->
<input type="hidden" id="click_order_type" value="团体课程">
<input type="hidden" id="order_type_child" value="1">
<input type="hidden" id="baseUrl" value="${baseUrl}">
<!-- mode end  -->
<link href="${baseUrl}css/plugins/clockpicker/clockpicker.css" rel="stylesheet">
<script src="${baseUrl}js/plugins/clockpicker/clockpicker.js"></script>
<script src="https://cdn.bootcss.com/bootstrap-modal/2.2.6/js/bootstrap-modal.js"></script>
<script>
    
    $(document).ready(function () {

        //时间选择
        $('input[name="daterange"]').daterangepicker({
            startDate: moment().startOf('day'),
            endDate: moment(),
            dateLimit: {days: 365}, //起止时间的最大间隔
            showDropdowns: true,
            showWeekNumbers: false, //是否显示第几周
            timePicker: false, //是否显示小时和分钟
            timePickerIncrement: 60, //时间的增量，单位为分钟
            timePicker12Hour: false, //是否使用12小时制来显示时间
            ranges: {
                //'最近1小时': [moment().subtract('hours',1), moment()],
                '今日': [moment().startOf('day'), moment()],
                '昨日': [moment().subtract('days', 1).startOf('day'), moment().subtract('days', 1).endOf('day')],
                '最近7日': [moment().subtract('days', 6), moment()],
                '最近30日': [moment().subtract('days', 30), moment()]
            },
            opens: 'right', //日期选择框的弹出位置
            buttonClasses: ['btn btn-default'],
            applyClass: 'btn-small btn-primary blue',
            cancelClass: 'btn-small',
            format: 'YYYY-MM-DD', //控件中from和to 显示的日期格式 YYYY-MM-DD HH:mm:ss
            separator: '~',
            locale: {
                applyLabel: '确定',
                cancelLabel: '取消',
                fromLabel: '起始时间',
                toLabel: '结束时间',
                customRangeLabel: '自定义',
                daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
                monthNames: ['一月', '二月', '三月', '四月', '五月', '六月',
                    '七月', '八月', '九月', '十月', '十一月', '十二月'],
                firstDay: 1
            }
        }, function (start, end, label) {//格式化日期显示框
            $('.date-rangepicker span').html(start.format('YYYY-MM-DD HH:mm:ss') + ' - ' + end.format('YYYY-MM-DD HH:mm:ss'));
        });
        
        Date.prototype.pattern = function (fmt) {

            var o = {
                "M+": this.getMonth() + 1, // 月份
                "d+": this.getDate(), // 日
                "h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, // 小时
                "H+": this.getHours(), // 小时
                "m+": this.getMinutes(), // 分
                "s+": this.getSeconds(), // 秒
                "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
                "S": this.getMilliseconds() // 毫秒
            };
            var week = {
                "0": "/u65e5",
                "1": "/u4e00",
                "2": "/u4e8c",
                "3": "/u4e09",
                "4": "/u56db",
                "5": "/u4e94",
                "6": "/u516d"
            };
            if (/(y+)/.test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            }
            if (/(E+)/.test(fmt)) {
                fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "/u661f/u671f" : "/u5468") : "") + week[this.getDay() + ""]);
            }
            for (var k in o) {
                if (new RegExp("(" + k + ")").test(fmt)) {
                    fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
                }
            }
            var str = fmt.lastIndexOf("N");
            if (str != -1) {
                return "";
            }

            return fmt;
        }
        
        $('.modal-loading').hide();	
        $("#btn_tuikuan").click(function(){
        	$('.modal-loading').hide();	
        	debugger;
        	var orderNumber = $("#orderNumber").val();
        	debugger;
        	if(orderNumber.length == 0){
        		swal("操作失败!", "订单号不能为空", "error");
        		return false;
        	}
        	swal({
                title: "确定退款\n订单号:" + orderNumber,
                text: "退款之后金额直接打入对方账户!",
                type: "warning",
                showCancelButton: true,
                cancelButtonText: "容我三思！",
                confirmButtonText: "确定!",
            }).then(function () {
                $.ajax({
                    url: "admin_createdRefun", 
                    data: {
                        "orderNumber": orderNumber,
                        "biaoshi": 1
                    },
                    type: 'post',
                    cache: false,
                    dataType: 'json',
                    success: function (data) {
                        if(data.ret == -1){
                        	swal("退款失败!", data.msg, "error");
                        }else{
                        	$("#orderNumber").val("");
                        	swal("退款成功", "金额已达到对方账户", "success");
                        	window.setTimeout(function () {
                            }, 1000);
                        }
                        
                    },
                    error: function () {
                    	$('.modal-loading').hide();	
                        swal("退款失败!", "请联系管理员处理", "error");
                    }
                });

            }, function () {
    		});
        });
        
        
        $("#btn_part").click(function(){
        	debugger;
        	var orderNumber = $("#part_orderNumber").val();
    		if(orderNumber.length == 0){
        		swal("操作失败!", "订单号不能为空!", "error");
        		return false;
        	}
        	getRefundHtml();
        });
    });
	
 // 获取参数
	function getParam() {
		var param = {
				"orderNumber":$("#part_orderNumber").val()
		}
        return param;
	}
 
    
    function Ajax(url,param,callbakc){
    	$.ajax({
			url : url,//
			data : param,
			type : 'post',
			cache : false,
			dataType : 'json',
			success : function(data) {
				$('.modal-loading').hide();
				if (data.ret == 0) {
					callbakc(data);
				}
			},
			error : function() {
				alert("页面数据加载失败！");
				$('.modal-loading').hide();
			}
		});
    }
    // 
    function getRefundHtml() {
		debugger;
		$('.modal-loading').show();
		// 异步请求数据
		var url = "get_refund_order_list";
		var param = getParam();
		debugger;
		$("#td1").empty();
		Ajax(url, param, setRefundHtml);
		

	}
    
 	// 渲染页面
	function setRefundHtml(datas) {
	    var params = getParam();
        $('#td1').empty();
        if (datas.ret == '-1') {
            swal("获取数据失败", date.msg, "error");
            return;
        }
        
        debugger;
        var orderInfo = datas.data.orderInfo;
        debugger;
        if(orderInfo == null || orderInfo == undefined || orderInfo == "undefined"){
        	swal("获取数据失败","该订单号查无数据！", "error");
            return;
        }
        var trtd = "";
        var orderRefund = '<button type="button" class="btn btn-default btn-sm" {cancelOrder}>'
			 +'<span class="glyphicon glyphicon-ban-circle"></span>退款</button>';
		var refundAmt = '<input type="number" class="form-control" id="{id}" value="退款金额">';
		var refundRemark = '<input type="text" class="form-control" id="{id}" value="退款说明">';
		
        $('#td1').empty();
        for (var i = 0; i < 1; i++) {
        	orderRefund = orderRefund.replace("{cancelOrder}","onclick=orderRefund('"+orderInfo.id+"','refundId"+(i+1)+"','"+orderInfo.amount+"','remark"+(i+1)+"')");
        	refundAmt 	= refundAmt.replace('{id}','refundId'+(i+1));
        	refundRemark 	= refundRemark.replace('{id}','remark'+(i+1));
        	trtd = trtd
        		   +'<tr>'
        		   +'<td>'+orderInfo.tradeId+'</td>'
        		   +'<td id="tk_th">'+new Date(orderInfo.payTime).pattern("yyyy-MM-dd HH:mm:ss")+'</td>'
        		   +'<td>'+orderInfo.orderNumber+'</td>'
        		   +'<td>'+orderInfo.amount+'</td>'
        		   +'<td>'+refundAmt+'</td>'
        		   +'<td>'+refundRemark+'</td>'
        		   +'<td>'+orderRefund+'</td>'
        		   +'</tr>';
            
        }
        $('#td1').append(trtd);
        $('.modal-loading').hide();
	}
   
 	function orderRefund(orderId,refundId,amount,remark){
 		var orderNumber = $("#part_orderNumber").val();
 		var refundAmt = $("#"+refundId).val();
 		debugger;
    	if(orderNumber.length == 0){
    		swal("操作失败!", "订单号不能为空!", "error");
    		return false;
    	}
    	if(refundAmt.length == 0){
    		swal("操作失败!", "退款金额不能为空!", "error");
    		return false;
    	}
    	if(parseFloat(amount)< parseFloat(refundAmt)){
    		swal("操作失败!", "退款金额不能大于实付金额!", "error");
    		return false;
    	}
    	if(refundAmt <= 0 || parseFloat(parseFloat) <= 0){
    		swal("操作失败!", "退款金额必须大于0!", "error");
    		return false;
    	}
    	var remark = $("#"+remark).val();
    	if(remark.length == 0){
    		swal("操作失败!", "请填写退款备注说明!", "error");
    		return false;
    	}
    	$('.modal-loading').hide();	
    	swal({
            title: "确定退款\n订单号:" + orderNumber,
            text: "退款之后金额直接打入对方账户!",
            type: "warning",
            showCancelButton: true,
            cancelButtonText: "容我三思！",
            confirmButtonText: "确定!",
        }).then(function () {
            $.ajax({
                url: "createdRefun.action", 
                data: {
                    "orderId": orderId,
                    "refundAmt": refundAmt,
                    "remark":remark
                },
                type: 'post',
                cache: false,
                dataType: 'json',
                success: function (data) {
                    if(data.ret == -1){
                    	swal("退款失败!", data.msg, "error");
                    }else{
                    	$("#orderNumber").val("");
                    	$("#td1").empty();
                    	$("#part_orderNumber").val("");
                    	swal("退款成功", "金额已达到对方账户", "success");
                    	
                    	
                    }
                    $('.modal-loading').hide();	
                },
                error: function () {
                	$('.modal-loading').hide();	
                    swal("退款失败!", "请联系管理员处理", "error");
                }
            });

        }, function () {
		});
 	}
</script>

</@body.page>
</html>